package com.example.system.service.sysuser.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.domain.R;
import com.example.common.core.domain.RedisUser;
import com.example.system.domain.sysuser.dto.SysUserDTO;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.enums.UserIdentity;
import com.example.common.redis.service.RedisService;
import com.example.common.security.exception.ServiceException;
import com.example.common.security.service.TokenService;
import com.example.system.domain.sysuser.SysUser;
import com.example.system.domain.sysuser.vo.LoginUserVO;
import com.example.system.mapper.sysuser.SysUserMapper;
import com.example.system.service.sysuser.ISysUserService;
import com.example.system.utils.BCryptUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
public class  SysUserServiceImpl implements ISysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RedisService redisService;

    @Value("${jwt.secret}")
    private String secret;
    @Override
    public R<String> login(String userAccount, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper.select(SysUser::getUserPassword, SysUser::getId,SysUser::getNickName).eq(SysUser::getUserAccount, userAccount));
        if (sysUser == null) {
            return R.fail(ResultCode.FAILED_LOGIN);
        }
        if (BCryptUtils.matchesPassword(password, sysUser.getUserPassword())) {
            return R.ok(tokenService.createToken(sysUser.getId(),secret, UserIdentity.ADMIN.getValue(),sysUser.getNickName(),null));
        }
        return R.fail(ResultCode.FAILED_LOGIN);
    }

    @Override
    public int add(SysUserDTO sysUserDTO) {
        if (sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUserAccount,sysUserDTO.getUserAccount())) != null) {
            throw new ServiceException(ResultCode.AILED_USER_EXISTS);
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserAccount(sysUserDTO.getUserAccount());
        sysUser.setUserPassword(BCryptUtils.encryptPassword(sysUserDTO.getUserPassword()));
        return sysUserMapper.insert(sysUser);

    }

    @Override
    public R<LoginUserVO> info(String token) {
        RedisUser redisUser = tokenService.getRedisUser(token,secret);
        LoginUserVO loginUserVO = new LoginUserVO();
        loginUserVO.setNickName(redisUser.getUserNick());
        return  R.ok(loginUserVO);
    }

    @Override
    public boolean exitLogin(String token) {
        return tokenService.exitLogin(token,secret);
    }




}
